package com.glasses.pandora.rest;

import com.glasses.pandora.domain.Report;
import com.glasses.pandora.service.ReportService;
import java.net.URI;
import java.util.List;
import javax.ejb.EJB;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 *
 * @author Jean-Luc Burot
 * @since 2014-09-30
 */
@Path("report")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ReportResource {
    @EJB
    private ReportService rs;
    
    @GET
    public Response findReports() {
        final List<Report> reports = rs.findAllReports();
        
        if(reports.size() <= 0)
            throw new NotFoundException("No reports found.");
        
        return Response.ok(new GenericEntity<List<Report>>(reports) {})
                       .build();
    }
    
    @GET
    @Path("{id:[1-9][0-9]*}")
    public Response findReport(@PathParam("id") Long id) {
        Report report = rs.findReport(id);
        
        if(report == null)
            throw new NotFoundException("No report found having the id " + id + ".");
        
        return Response.ok(report)
                       .build();
    }
    
    @POST
    public Response createReport(@Valid Report report) {
        rs.saveReport(report);
        
        return Response.created(URI.create("/" + report.getId()))
                       .build();
    }
}
